package std.game.objects.views;

import static net.wimpi.telnetd.io.terminal.ColorHelper.CYAN;
import static net.wimpi.telnetd.io.terminal.ColorHelper.GREEN;
import static net.wimpi.telnetd.io.terminal.ColorHelper.WHITE;
import static net.wimpi.telnetd.io.terminal.ColorHelper.colorizeText;


import org.apache.log4j.Logger;


import org.groovymud.object.alive.Alive
import org.groovymud.object.room.Room
import org.groovymud.shell.security.MudPrincipal
import std.game.objects.containers.RoomImpl
import static utils.WordUtils.pluralize

public class RoomView<T extends Room> extends ContainerMudObjectView<RoomImpl> {

    private transient static final Logger logger = Logger.getLogger(RoomView.class);

    public void writeLookHeader(Alive looker, T target) {
        def stream = looker.getTerminalOutput();

        try {

            boolean isCreator = looker.metaClass.respondsTo("getSubject") ? new MudPrincipal("trialcreator").implies(looker.subject) : false;

            stream.write(colorizeText(target.name, GREEN));
            if (isCreator) {
                stream.write "(${target.getClass().protectionDomain.codeSource.location.path})"
            }
            stream.writeln ""
        } catch (IOException e) {
            logger.error(e, e);
        }
    }

    public void writeLookBody(Alive looker, T target) {
        final def stream = looker.getTerminalOutput();
        final containerInventoryHandler = target.getInventoryHandler()
        final mudObjectsMap = containerInventoryHandler.getMudObjectsMap()
        final notAlive = mudObjectsMap.findAll {
            !it.value instanceof Alive

        }
        final def alive = mudObjectsMap.findAll {
            it.value instanceof Alive
        };
        try {
            writeDynamicText(looker, target, target.getDescription());

            if (notAlive.size() > 0) {
                stream.write "You see: "
                super.writeLookBody(looker, target)
                stream.writeln(".");
            }
            final String aliveContents = contentHelper.getContentsDescription(alive, looker);

            if (alive.size() > 0) {
                stream.write(net.wimpi.telnetd.io.terminal.ColorHelper.colorizeText(aliveContents, GREEN));
                stream.writeln(alive.size() > 1 ? " are here." : " is here.");
            }
        } catch (IOException e) {
            logger.error(e, e);
        }
    }

    public void writeLookFooter(Alive looker, T target) {
        List exits = target.getExitInventory().getMudObjects() as List;
        def stream = looker.getTerminalOutput();
        if (exits.size() > 0) {
            final text = colorizeText("""There ${(exits.size() == 1 ? 'is' : 'are')}
 ${pluralize('exit', exits.size())}: """, CYAN)
            stream.write(text)
            def coloredExits = []
            exits.each { exit ->
                coloredExits << colorizeText(exit.getDirection(), WHITE)
            }
            stream.writeln("${coloredExits.join(", ")}.")
        } else {
            stream.writeln("There are no exits.");
        }
    }
}
